package stu.khy.dao.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;

import stu.khy.comm.Constants;
import stu.khy.dao.EmployeeDao;
import stu.khy.entity.Employee;
@Repository
public class EmployeeDaoImpl extends BaseHibernateDaoSupport<Employee> implements EmployeeDao {
	@Autowired
	private HibernateTemplate hibernateTemplate;
	@SuppressWarnings("deprecation")
	@Override
	public Employee loginEmp(String name) {
		return (Employee)hibernateTemplate.findByNamedParam("from Employee where name=:name", "name", name).get(0);
	}

	@Override
	public int registerEmp(Employee employee) {
		 int a=(int)hibernateTemplate.save(employee);
		  hibernateTemplate.flush();
			return a;
	}

	@Override
	public Employee findEmpBySn(String sn) {
		return (Employee) hibernateTemplate.findByNamedParam("from Employee where sn=:sn", "sn", sn).get(0);
	}

	@Override
	public Employee getManager(Employee employee) {
		String hql = "from Employee e where e.status = ? and e.sysDepartment.id = ? and e.sysPosition.nameCn = ?";
		List<Employee> emps=this.find(hql, Constants.EMPLOYEE_STAY,employee.getSysDepartment().getId(),Constants.POSITION_FN);
		if(emps == null || emps.size() == 0 ) {
			return null;
		}
		return emps.get(0);
	}

}
